﻿var el_drag;
var el_drop;
function XRight() {
	if (el("body").className == "XRight") {
		el("body").className = "";
	} else {
		el("body").className = "XRight";
	}
}
function productDrag(evt) {

	evt.dataTransfer.effectAllowed = 'move';
	el_drag = evt.srcElement;
	return false;
}
function dragOver(evt) {
	if (evt.preventDefault) {
		evt.preventDefault(); // Necessary. Allows us to drop.
	}
	if (el_drop != el_drag.parentElement) {
		evt.dataTransfer.dropEffect = 'move';
	} else {
		evt.dataTransfer.dropEffect = 'none';
	}
	return false;
}
function dragEnter(evt) {
	if (evt.currentTarget.id == "cen") {
		el_drop = evt.currentTarget;
	} else {
		el_drop = el("#rigMid");
	}
	return false;
}
function drop(evt) {
	if (el_drop != el_drag.parentElement) {
		addToCar(el_drag);
	}
}
function addToCar(element) {
	var id = element.getAttribute("data-id");
	if (element.parentElement.id == "cen") {
		if (els("#rig div[data-id=\"" + id + "\"]").length > 0) {
			var elQuatity = el("#rig div[data-id=\"" + id + "\"] .quantity");
			elQuatity.value = eval(elQuatity.value) + 1;
			calTotal(element);
		} else {
			var elNew = document.createElement("div");
			el("#rig").children[2].appendChild(elNew);
			elNew.outerHTML = element.outerHTML;
			if (elNew != el("#rig").children[2].children[0])
				el("#rig").children[2].insertBefore(elNew, el("#rig").children[2].children[0]);
		}
	} else {
		el("#rig").children[2].removeChild(element);
	}
	calSum();
}
function calSum() {
	var elSum = el("#rigTop #Total");
	var sum = 0;
	var elTotals = els("#rig .product .total");
	for (var i = 0; i < elTotals.length; i++) {
		sum += eval(elTotals[i].innerHTML);
	}
	elSum.innerHTML = sum.formatMoney();
}
function calTotal(elProduct) {
	var id = elProduct.getAttribute("data-id");
	var elQuatity = el("#rig div[data-id=\"" + id + "\"] .quantity");
	var elPrice = el("#rig div[data-id=\"" + id + "\"] .price");
	var elTotal = el("#rig div[data-id=\"" + id + "\"] .total");
	var quantity = elQuatity.value;
	elTotal.innerHTML = quantity * eval(elPrice.innerHTML);
}
function quantityChange(evt) {
	calTotal(evt.srcElement.parentElement);
	calSum();
}
function showDetail(element, evt) {
	var url = element.getAttribute("data-id");
	url = urlHost + "Order/Detail?id=" + url;
	openPopup("#detail", url);
}
function btnOrderBuy(evt) {
	var strs = new Array();
	var elProducts = els("#rig .product");
	for (var i = 0; i < elProducts.length; i++) {
		var id = elProducts[i].getAttribute("data-id");
		var elQuantity = el("#rig div[data-id=\"" + id + "\"] .quantity");
		strs.push(id + ":" + elQuantity.value);
	}
	var str = strs.join("|");
	var str2 = window.location.search;
	str2 = str2.replace("?", "");
	strs = str2.split("&");
	window.location = urlHost + "Order/Create?strOD=" + str+"&"+strs[0];
}
//window.addEventListener("load", function () {
//	var elsi=els("#cen.home>div");
//	for (i=0;i<elsi.length;i++) {
//		var element = document.createElement("div");
//		element.setAttribute("onclick", "");
//		elsi[i].insertBefore(element, elsi[i].children[2]);
//		element = document.createElement("div");
//		element.setAttribute("onclick", "showDetail(this.parentElement);");
//		elsi[i].insertBefore(element, elsi[i].children[4]);
//		elsi[i].className = "product";
//	}
//});